/*
 * Copyright (c) 2012 Andrea De Rinaldis <ozioso at ipignoli.dyndns.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dyndns.ipignoli.droidcruise.notifications;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.dyndns.ipignoli.droidcruise.util.MyContext;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.speech.tts.TextToSpeech;


public class MyNotificationManager{
  private static MyNotificationManager instance;

  public static MyNotificationManager getInstance(){
    if(instance == null)
      instance = new MyNotificationManager();
    return instance;
  }

  private NotificationManager notificationManager;
  private TextToSpeech        tts;
  private boolean             mute;
  private Set<MyNotification> notifications;

  private MyNotificationManager(){
    notificationManager =
        (NotificationManager)MyContext.getInstance().getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    tts = null;
    mute = false;
    notifications = new HashSet<MyNotification>();
  }

  public synchronized TextToSpeech getTts(){
    return tts;
  }

  public synchronized void setTts(TextToSpeech tts){
    this.tts = tts;
  }

  public synchronized boolean isMute(){
    return mute;
  }

  public synchronized void setMuteOrUnmute(){
    mute = !mute;
  }

  private String transformForTTS(CharSequence text){
    if(text == null)
      return "";
    String ret =
        text.toString().replace('-', ' ').toLowerCase().replace("gps", "GPS");
    if(getTts() == null || getTts().getLanguage() == null
        || getTts().getLanguage().getLanguage() == null)
      return ret;
    if(getTts().getLanguage().getLanguage().equals("it"))
      ret.replace("tutor", "tùtor").replace("autovelox", "autovèlox")
          .replace("satellit", "satèllit");
    return ret;
  }

  public void notify(int id, MyNotification notification){
    if(notifications.contains(notification))
      return;
    int saveDefaults = notification.defaults;
    if(!isMute())
      if(getTts() == null)
        notification.defaults |= Notification.DEFAULT_SOUND;
      else{
        getTts().speak(transformForTTS(notification.tickerText),
            TextToSpeech.QUEUE_ADD, null);
        getTts().speak(transformForTTS(notification.getTitle()),
            TextToSpeech.QUEUE_ADD, null);
        getTts().speak(transformForTTS(notification.getText()),
            TextToSpeech.QUEUE_ADD, null);
      }
    notificationManager.notify(id, notification);
    notification.defaults = saveDefaults;
    notifications.add(notification);
  }

  public void cancel(int id, MyNotification notification){
    notificationManager.cancel(id);
    notifications.remove(notification);
  }

  public void clearAll(){
    for(Iterator<MyNotification> it=notifications.iterator();it.hasNext();)
      notificationManager.cancel(it.next().getId());
    notifications = new HashSet<MyNotification>();
  }
}
